using System;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestDMPropRuleParameterControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        [Test]
        public void Test_CreateControl_NullFactory_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                new DMPropRuleParameterControl(null);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("controlFactory", ex.ParamName);
            }
            //---------------Test Result -----------------------
        }

        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
            Assert.IsNull(control.BusinessObject);
            Assert.IsNull(control.PropertyRule);
        }

        [Test]
        public void TestSetBusinessObject_ToPropRule()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropRule dmPropRule = TestUtilsShared.CreateSavedPropRuleInfo(property);
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            control.BusinessObject = dmPropRule;
            //---------------Test Result -----------------------
            Assert.AreSame(dmPropRule, control.PropertyRule);
            Assert.AreSame(dmPropRule, control.BusinessObject);
            Assert.AreEqual(dmPropRule.PropRuleParameters.Count, control.PropRuleParameters.Count);
        }

        [Test]
        public void TestSetPropRuleInfo()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropRule dmPropRule = TestUtilsShared.CreateSavedPropRuleInfo(property);
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            control.PropertyRule = dmPropRule;
            //---------------Test Result -----------------------
            Assert.AreSame(dmPropRule, control.PropertyRule);
            Assert.AreSame(dmPropRule, control.BusinessObject);
            Assert.AreEqual(dmPropRule.PropRuleParameters.Count, control.PropRuleParameters.Count);
        }

        [Test]
        public void TestSetEnables()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropRule dmPropRule = TestUtilsShared.CreateSavedPropRuleInfo(property);
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            //---------------Execute Test ----------------------
            control.BusinessObject = dmPropRule;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
        }

        [Test]
        public void TestSetToNullDisables()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropRule dmPropRule = TestUtilsShared.CreateSavedPropRuleInfo(property);
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            control.PropertyRule = dmPropRule;
            //---------------Execute Test ----------------------
            control.BusinessObject = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void Test_Constructor_CannotAddNewRows()
        {
            //---------------Set up test pack-------------------
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            IEditableGridControl editableGridControl = (IEditableGridControl) control.SelectorControl;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool allowUserToAddRows = editableGridControl.Grid.AllowUserToAddRows;
            //---------------Test Result -----------------------
            Assert.IsFalse(allowUserToAddRows);
        }
        [Test]
        public void Test_SetPropRule_CannotAddNewRows()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropRule dmPropRule = TestUtilsShared.CreateSavedPropRuleInfo(property);
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            
            IEditableGridControl editableGridControl = (IEditableGridControl) control.SelectorControl;
            //---------------Assert Precondition----------------
            Assert.IsFalse(editableGridControl.Grid.AllowUserToAddRows);
            //---------------Execute Test ----------------------
            control.BusinessObject = dmPropRule;
            //---------------Test Result -----------------------
            Assert.IsFalse(editableGridControl.Grid.AllowUserToAddRows);
        }

        [Test]
        public void Test_Constructor_CannotDeleteRows()
        {
            //---------------Set up test pack-------------------
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            IEditableGridControl editableGridControl = (IEditableGridControl) control.SelectorControl;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool allowUserToDeleteRows = editableGridControl.Grid.AllowUserToDeleteRows;
            //---------------Test Result -----------------------
            Assert.IsFalse(allowUserToDeleteRows);
        }
        [Test]
        public void Test_SetPropRule_CannotDeleteRows()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            DMProperty property = TestUtilsShared.CreateSavedDMProperty(bo);
            DMPropRule dmPropRule = TestUtilsShared.CreateSavedPropRuleInfo(property);
            DMPropRuleParameterControl control = new DMPropRuleParameterControl(new ControlFactoryWin());
            
            IEditableGridControl editableGridControl = (IEditableGridControl) control.SelectorControl;
            //---------------Assert Precondition----------------
            Assert.IsFalse(editableGridControl.Grid.AllowUserToDeleteRows);
            //---------------Execute Test ----------------------
            control.BusinessObject = dmPropRule;
            //---------------Test Result -----------------------
            Assert.IsFalse(editableGridControl.Grid.AllowUserToDeleteRows);
        }
    }
}